package com.dule.Medic.service_hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.dule.Medic.beans.MedEntryBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.MedEntry;
import com.dule.Medic.model.Patient;
import com.dule.Medic.service.MedEntryService;

public class MedEntryHibernateService implements MedEntryService 
{
	private final Session session;
	
	public MedEntryHibernateService(Session session) {
		this.session = session;
	}

	public Long save(MedEntry medEntry) {
		Long id = (Long) session.save(medEntry);
		return  id;
	}
	
	public boolean update(MedEntry medEntry) 
	{
		session.merge(medEntry);
		return true;
	}

	@SuppressWarnings("unchecked")
	public List<MedEntry> retrieve(Patient patient) 
	{
		List<MedEntry> medEntrys = session.createCriteria(MedEntryBean.class).add(Restrictions.eq("patient.id", patient.getId())).list();
		return medEntrys;
	}
	@SuppressWarnings("unchecked")
	public List<MedEntry> retrieve(Doctor doctor) 
	{
		List<MedEntry> medEntrys = session.createCriteria(MedEntryBean.class).add(Restrictions.eq("doctor.id", doctor.getId())).list();
		return medEntrys;
	}
	
	public boolean delete(MedEntry medEntry) 
	{
		session.delete(medEntry);
		return true;
	}
	
	public MedEntry retrieveOne(Long Id) 
	{
		Criteria crit = session.createCriteria(MedEntryBean.class).add(Restrictions.eq("id", Id));    
		return (MedEntry) crit.uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<MedEntry> retrieveAll() 
	{
		List<MedEntry> medentrys=session.createCriteria(MedEntryBean.class).list(); 
		return medentrys;
	}

}
